<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" size="mini" @click="handleedit('add')"
          >新增</el-button
        >
        <el-button type="danger" size="mini" @click="handleDelete('all')"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="supplierList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="序号" align="center" prop="id" width="100">
        <template slot-scope="scope">
          <div>
            {{
              scope.$index +
              (queryParams.pageNum - 1) * queryParams.pageSize +
              1
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="标题"
        align="center"
        prop="title"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column label="可见权限" align="center" prop="title">
        <template slot-scope="scope">
          <div>
            {{
              scope.row.title == "1"
                ? "中国"
                : scope.row.title == "2"
                ? "非洲"
                : "所有"
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="是否置顶"
        align="center"
        prop="sort"
        v-if="routername == 'videolist'"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.title == "1" ? "是" : "否" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="封面图片"
        align="center"
        prop="img"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <div v-if="Array.isArray(scope.row.img)">
            <el-image
              style="width: 30px; height: 30px"
              :src="scope.row.img[0].url"
              :preview-src-list="[scope.row.img[0].url]"
            >
            </el-image>
          </div>
          <div v-else>
            <el-image
              style="width: 30px; height: 30px"
              :src="scope.row.img"
              :preview-src-list="[scope.row.img]"
            >
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="浏览量"
        align="center"
        prop="lookSize"
        :show-overflow-tooltip="true"
        v-if="routername == 'chinaafic'"
      >
      </el-table-column>
      <el-table-column
        label="视频时长"
        align="center"
        prop="videoTime"
        :show-overflow-tooltip="true"
        v-if="routername == 'videolist'"
      >
      </el-table-column>
      <el-table-column
        label="是否启用"
        align="center"
        prop="status"
        width="100"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status == '1'"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="changestate(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        prop="opration"
        width="200"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            @click="handleedit('edit', scope.row)"
            >修改</el-button
          > -->
          <el-button
            type="text"
            size="mini"
            @click="handleedit('more', scope.row)"
            >详情</el-button
          >
          <el-button
            type="text"
            size="mini"
            icon="el-icon-delete"
            style="color: red"
            @click="handleDelete('row', scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改公告对话框 -->
    <el-dialog
      :title="title"
      :visible="open"
      width="800px"
      append-to-body
      :before-close="handeclose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题:" prop="title">
              <el-input
                v-model="form.title"
                :disabled="isdisable"
                placeholder="请输入"
                style="width: 90%"
              />
            </el-form-item>
            <el-form-item
              label="视频链接:"
              prop="videoUrl"
              v-if="routername == 'videolist'"
            >
              <el-input
                :disabled="isdisable"
                v-model="form.videoUrl"
                placeholder="请输入"
                style="width: 90%"
              />
            </el-form-item>
            <el-form-item
              label="时长:"
              prop="videoTime"
              v-if="routername == 'videolist'"
            >
              <el-input
                v-model="form.videoTime"
                placeholder="请输入"
                style="width: 80%"
                :disabled="isdisable"
              />
              <span> 如 3'23''</span>
            </el-form-item>
            <el-form-item
              label="内容:"
              prop="content"
              v-if="routername != 'videolist'"
            >
              <editor
                v-if="!isdisable"
                v-model="form.content"
                :min-height="192"
                style="width: 90%"
              />
              <div style="content-box" v-else>
                <div
                  style="width: 90%; line-height: 15px"
                  v-html="form.content"
                ></div>
              </div>
            </el-form-item>
            <el-form-item label="封面图:" prop="img" class="formdata">
              <ImageUpload
                :limit="1"
                :fileSize="fileSize"
                :isShowTip="false"
                uploadtype="image"
                :fileList.sync="form.img"
                ref="tableupload"
                v-if="!isdisable"
              />
              <div v-else>
                <el-image
                  style="width: 50px; height: 50px"
                  :src="form.img[0].url || form.img"
                  :preview-src-list="[form.img[0].url || form.img]"
                >
                </el-image>
              </div>
            </el-form-item>
            <el-form-item label="查看次数:" prop="lookSize">
              <el-input-number
                v-model="form.lookSize"
                :min="1"
                label="请输入"
                style="width: 200px"
                :disabled="isdisable"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="可见权限:" prop="isShow">
              <el-radio-group v-model="form.isShow" :disabled="isdisable">
                <el-radio
                  v-for="(item, index) in jurisdictionoption"
                  :key="index"
                  :label="item.value"
                  >{{ item.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
            <el-form-item
              label="是否置顶:"
              prop="sort"
              v-if="routername == 'videolist'"
            >
              <el-radio v-model="form.sort" :disabled="isdisable" :label="'0'"
                >否</el-radio
              >
              <el-radio v-model="form.sort" :disabled="isdisable" :label="'1'"
                >是</el-radio
              >
            </el-form-item>
            <el-form-item label="状态:" prop="status">
              <el-radio v-model="form.status" :disabled="isdisable" :label="'0'"
                >禁用</el-radio
              >
              <el-radio v-model="form.status" :disabled="isdisable" :label="'1'"
                >启用</el-radio
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  wisdomlist,
  infolist,
  videolist,
  addwisdom,
  addinfo,
  addvideo,
  delwisdom,
  delvideo,
  delinfo,
  editwisdom,
  editvideo,
  editinfo,
} from "@/api/website/investment";
import Editor from "@/components/Editor";
import selfDirective from "@/utils/selfDirective";
import FileUpload from "@/components/FileUpload/index";
import ImageUpload from "@/components/ImageUpload/index";
import { Base64 } from "js-base64";

export default {
  name: "supplier",
  components: {
    Editor,
    FileUpload,
    ImageUpload,
  },
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };
    return {
      // 遮罩层
      loading: false,
      routername: "",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      fileSize: 0.5,
      // 公告表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        withdrawal: "0",
      },
      // 表单参数
      form: {
        title: "",
        img: [],
        isShow: "0",
      },
      // 表单校验
      rules: {
        title: [
          {
            required: true,
            message: "请选择审核结果",
            trigger: "change",
          },
        ],
        videoUrl: [
          {
            required: true,
            message: "视频链接不能为空",
            trigger: "change",
          },
        ],
        videoTime: [
          {
            required: true,
            message: "时长不能为空",
            trigger: "change",
          },
        ],
        img: [
          {
            required: true,
            message: "封面图片不能为空",
            trigger: "change",
          },
        ],
        content: [
          {
            required: true,
            message: "内容不能为空",
            trigger: "change",
          },
        ],
        isShow: [
          {
            required: true,
            message: "请选择可见权限",
            trigger: "change",
          },
        ],
        sort: [
          {
            required: true,
            message: "请选择是否置顶",
            trigger: "change",
          },
        ],
        status: [
          {
            required: true,
            message: "请选择状态",
            trigger: "change",
          },
        ],
        lookSize: [
          {
            required: true,
            message: "查看次数不能为空",
            trigger: "change",
          },
        ],
      },
      issupplier: true,
      limit: 5,
      fileSize: 20,
      isShowTip: false,
      value: [],
      nationoptions: [],
      categoryoptions: [],
      companyoptions: [],
      gysTypeOptions: [],
      payEemerchantNooptions: [],
      selection: [],
      isdisable: false,
      isdisabletwo: false,
      msgvisible: false,
      //银行信息
      bankmsg: {
        bankname: "",
        accountname: "",
        bankaccount: "",
        IBAN: "",
        SwiftCode: "",
        branchCode: "",
        bankaddress: "",
      },
      statusoptions: [
        {
          dictValue: "1",
          dictLabel: "已启用",
        },
        {
          dictValue: "2",
          dictLabel: "已禁用",
        },
      ],
      jurisdictionoption: [
        {
          label: "所有",
          value: "0",
        },
        {
          label: "中国",
          value: "1",
        },
        {
          label: "非洲",
          value: "2",
        },
      ],
    };
  },
  created() {
    if (this.$route.name == "/website/videolist") {
      this.routername = "videolist";
    } else if (this.$route.name == "/website/investment") {
      this.routername = "investment";
    } else if (this.$route.name == "/website/chinaafic") {
      this.routername = "chinaafic";
    }
    this.getList();
    this.getDicts("gys_type").then((response) => {
      this.categoryoptions = response.data;
    });
    this.getDicts("gys-gj").then((response) => {
      this.nationoptions = response.data;
    });
    this.getDicts("pay-eemer-chantNo").then((response) => {
      this.payEemerchantNooptions = response.data;
    });
  },
  methods: {
    getbankaccount(row) {
      let bankaccount = "";
      if (row.bankaccount) {
        bankaccount =
          "*******" +
          row.bankaccount.slice(
            row.bankaccount.length - 4,
            row.bankaccount.length
          );
      } else {
        bankaccount = "";
      }
      return bankaccount;
    },
    handleSelectionChange(val) {
      this.selection = val;
    },
    /** 查询公告列表 */
    handelcopy() {
      copysupplier().then((res) => {
        this.msgSuccess("复制成功");
      });
    },
    /** 查询公告列表 */
    changeStatus(value) {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询公告列表 */
    handlecord(row) {
      this.msgvisible = true;
      this.bankmsg = row;
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      if (this.$route.name == "/website/videolist") {
        //视频
        videolist(this.queryParams).then((response) => {
          this.gettabledata(response);
        });
      } else if (this.$route.name == "/website/investment") {
        //投资
        infolist(this.queryParams).then((response) => {
          this.gettabledata(response);
        });
      } else if (this.$route.name == "/website/chinaafic") {
        //中非
        wisdomlist(this.queryParams).then((response) => {
          this.gettabledata(response);
        });
      }
    },
    gettabledata(response) {
      var supplierList = response.rows || [];
      supplierList.map((item) => {
        item.img = this.isJSON(item.img) ? JSON.parse(item.img) : item.img;
        item.isShow = item.isShow
          ? item.isShow.toString()
          : item.isShow == 0
          ? "0"
          : item.isShow;
        item.status = item.status
          ? item.status.toString()
          : item.status == 0
          ? "0"
          : item.status;
      });
      this.supplierList = supplierList;
      this.total = response.total;
      this.loading = false;
    },
    isJSON(str) {
      if (typeof str == "string") {
        try {
          JSON.parse(str);
          return true;
        } catch (e) {
          return false;
        }
      }
    },
    // 公告状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 公告状态字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.noticeType);
    },
    // 取消按钮
    cancel() {
      if (this.title == "详情") {
        this.open = false;
      } else {
        this.open = false;
        this.reset();
      }
    },
    // 表单重置
    reset() {
      this.resetForm("form");
      this.form = {
        id: undefined,
        img: [],
        isShow: "0",
        status: "1",
        sort: "1",
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm: function () {
      if (this.title == "详情") {
        this.open = false;
        return;
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            editsupplier(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            let content = Base64.encode(this.form.content);
            let img = JSON.stringify(this.form.img);
            if (this.$route.name == "/website/videolist") {
              //视频
              let obj = {
                ...this.form,
                img: img,
              };
              addvideo(obj).then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            } else if (this.$route.name == "/website/investment") {
              //投资
              let obj = {
                ...this.form,
                img: img,
                content: content,
              };
              addinfo(obj).then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            } else if (this.$route.name == "/website/chinaafic") {
              //中非
              let obj = {
                ...this.form,
                img: img,
                content: content,
              };
              addwisdom(obj).then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        }
      });
    },
    handleedit(type, row) {
      this.open = true;
      if (type == "edit") {
        this.reset();
        this.form = row;
        this.title = "修改";
        this.isdisable = false;
        this.isdisabletwo = true;
      } else if (type == "add") {
        this.reset();
        this.title = "新增";
        this.isdisable = false;
      } else if (type == "more") {
        this.title = "详情";
        this.form = {
          ...row,
          content: row.content,
        };
        this.isdisable = true;
        this.isdisabletwo = true;
      }
    },
    handeclose() {
      this.open = false;
    },
    getdictlabel(row, nationoptions, name) {
      let str = "";
      nationoptions.map((item) => {
        if (item.dictValue == row[name]) {
          str = item.dictLabel;
        }
      });
      return str;
    },
    /** 删除按钮操作 */
    handleDelete(type, row) {
      let ids;
      if (type == "all") {
        let selection = this.selection;
        if (selection.length == 0) {
          this.$message.error("请选择一条删除数据");
          return;
        }
        let idsarr = [];
        selection.map((item) => {
          idsarr.push(item.id);
        });
        ids = idsarr.join(",");
      } else {
        ids = row.id;
      }
      var that = this;
      this.$confirm("是否确认删除该数据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          that.selection = [];
          that.$route.name;
          if (that.$route.name == "/website/videolist") {
            ids;
            //视频
            return delvideo(ids);
          } else if (that.$route.name == "/website/investment") {
            //投资
            return delinfo(ids);
          } else if (that.$route.name == "/website/chinaafic") {
            //中非
            return delwisdom(ids);
          }
        })
        .then(() => {
          this.getList();
          this.$message.success("删除成功");
        })
        .catch(() => {});
    },
    changestate(row) {
      var that = this;
      const statusstr = row.status == 1 ? "禁用" : "启用";
      this.$confirm("是否确认" + statusstr + "该供应商?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        var obj = {
          id: row.id,
          status: row.status == 1 ? 0 : 1,
        };
        if (that.$route.name == "/website/videolist") {
          //视频
          editvideo(obj).then((response) => {
            that.$message.success(statusstr + "成功");
            that.getList();
          });
        } else if (that.$route.name == "/website/investment") {
          //投资
          editinfo(obj).then((response) => {
            that.$message.success(statusstr + "成功");
            that.getList();
          });
        } else if (that.$route.name == "/website/chinaafic") {
          //中非
          editwisdom(obj).then((response) => {
            that.$message.success(statusstr + "成功");
            that.getList();
          });
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
.radio-box {
  margin-bottom: 10px;
}
.stock {
  text-decoration: underline;
}
.copy {
  color: #999;
  cursor: pointer;
}
.box-card {
  margin-top: 20px;
  .card-title {
    font-weight: 700;
    font-style: normal;
    color: #555555;
  }
  .baseinfo {
    width: 100%;
    height: 40px;
    line-height: 40px;
    color: #999;
    background-color: #f2f2f2;
    padding-left: 10px;
    border-radius: 5px;
    margin-top: 10px;
  }
}
.dialog-footer {
  display: flex;
  justify-content: center;
  align-items: center;
}
.lookcard {
  text-decoration: underline;
  color: #1890ff;
  cursor: pointer;
}
.banktable {
  td {
    padding: 5px;
  }
}

::v-deep {
  .el-table .cell {
    white-space: pre-line !important; /*保留换行符*/
  }

  .el-form-item__content {
    line-height: 30px;
  }
}
.formdata ::v-deep {
  .el-form-item__label {
    white-space: pre-line !important; /*保留换行符*/
    line-height: 15px;
  }
}
.content-box {
  border: 1px solid #999;
  padding: 5px;
  height: 200px;
  overflow-y: auto;
  line-height: 15px;
}
</style>